package com.ruoyi.web.controller.baseInfo;

import com.ruoyi.baseInfo.domain.ClientsInfo;
import com.ruoyi.baseInfo.service.ClientsInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/clients")
public class ClientsInfoController extends BaseController {
    @Autowired
    ClientsInfoService clientsInfoService;

    @PreAuthorize("@ss.hasPermi('baseInfo:clients:list')")
    @GetMapping("/list")
    public AjaxResult list(ClientsInfo clientsInfo)
    {
        List<ClientsInfo> list = clientsInfoService.selectClientList(clientsInfo);
        return success(list);
    }

    /**
     * 查询公司列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('baseInfo:clients:list')")
    @GetMapping("/list/{companyId}")
    public AjaxResult excludeChild(@PathVariable(value = "companyId", required = false) Long companyId)
    {
        List<ClientsInfo> clients = clientsInfoService.selectClientList(new ClientsInfo());
        clients.removeIf(d -> d.getCompanyId().intValue() == companyId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), companyId + ""));
        return success(clients);
    }

    /**
     * 新增公司
     */
    @PreAuthorize("@ss.hasPermi('baseInfo:clients:add')")
    @Log(title = "企业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ClientsInfo clientsInfo)
    {
        if (UserConstants.NOT_UNIQUE.equals(clientsInfoService.checkCompanyNameUnique(clientsInfo)))
        {
            return error("新增企业'" + clientsInfo.getCompanyName() + "'失败，企业名称已存在");
        }
        clientsInfo.setCreateBy(getUsername());
        return toAjax(clientsInfoService.insertClient(clientsInfo));
    }

    /**
     * 删除公司
     */
    @PreAuthorize("@ss.hasPermi('baseInfo:clients:remove')")
    @Log(title = "企业管理",businessType = BusinessType.DELETE)
    @DeleteMapping("/{companyId}")
    public AjaxResult remove(@PathVariable Long companyId){
        if (clientsInfoService.hasChildById(companyId)){
            return warn("存在下级公司,不允许删除");
        }
        clientsInfoService.checkClientDataScope(companyId);
        return toAjax(clientsInfoService.deleteClientById(companyId));
        }
    /**
     * 修改公司
     */
    @PreAuthorize("@ss.hasPermi('baseInfo:clients:update')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@Validated @RequestBody ClientsInfo clientsInfo){
        Long companyId = clientsInfo.getCompanyId();
        clientsInfoService.checkClientDataScope(companyId);
        if (UserConstants.NOT_UNIQUE.equals(clientsInfoService.checkCompanyNameUnique(clientsInfo)))
        {
            return error("修改"+clientsInfo.getCompanyName()+"失败,公司名称已存在");
        }
        else if (clientsInfo.getParentId().equals(companyId))
        {
            return error("修改"+clientsInfo.getCompanyName()+"失败,上级公司名不能是自己");
        }
        else if (StringUtils.equals(UserConstants.EXCEPTION,clientsInfo.getType()) && clientsInfoService.selectNormalChildrenById(companyId)>0)
        {
            return error("该公司包含未停用的子公司");
        }
        return toAjax(clientsInfoService.updateClient(clientsInfo));
    }

    /**
     * 根据公司Id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('baseInfo:clients:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable Long companyId)
    {
        clientsInfoService.checkClientDataScope(companyId);
        return success(clientsInfoService.selectCompanyById(companyId));
    }
}
